package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.request.RecordListRequest;
import com.yanchangapp.model.response.RecordListResponse;
import com.yanchangapp.ui.view.GoodsRecordFgView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-23.
 * description:产品记录列表p层
 */
public class GoodsRecordFgPresenter extends BasePresenter<GoodsRecordFgView> {

    public GoodsRecordFgPresenter(BaseActivity context) {
        super(context);
    }

    public void queryRecordsList(String status,int curPage,boolean isPulling){
        // 如果是第一页，并且下拉下来的 展示遮盖层
        if (getView() != null && curPage == 1 && !isPulling){
            getView().showCoverDialog();
        }
        RecordListRequest request = new RecordListRequest(status,curPage);
        HeadApiRetrofit.getInstance().queryRecordList(request)
            .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RecordListResponse>() {
                    @Override
                    public void onNext(RecordListResponse recordListResponse) {
                        if (getView() != null){
                            if (curPage == 1 && !isPulling){
                                getView().hideCoverDialog();
                            }
                            if (AppConstant.REQUEST_SUCCESS == recordListResponse.getCode()){
                                getView().queryRecordListSuccess(recordListResponse.getData().getList());
                            }else {
                                getView().queryRecordListFailure(recordListResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            if (curPage == 1 && !isPulling){
                                getView().hideCoverDialog();
                            }
                            getView().queryRecordListFailure(exception.getErrorInfo());
                        }
                    }
                });
    }
}
